﻿/*
 * Created by SharpDevelop.
 * User: Intel
 * Date: 19.11.2014
 * Time: 15:38
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
namespace Region.Utils
{
  /// <summary>
  /// Description of SQLView.
  /// </summary>
  public partial class SQLView : Form
  {
    
    Engine m_engine;
    List<Engine.SQLEventArgs> list = new List<Engine.SQLEventArgs>();
    void EventHandlerOnSQL(Engine.SQLEventArgs ea)
    {
      /*listBox1.Items.Add("["+ea.ElapsedMilliseconds.ToString()+"] "+ea.SQL);
      list.Add(ea);*/
    }
    public SQLView(Engine engine)
    {
      m_engine = engine;
      //
      // The InitializeComponent() call is required for Windows Forms designer support.
      //
      InitializeComponent();
      m_engine.OnSQL += EventHandlerOnSQL;
      //
      // TODO: Add constructor code after the InitializeComponent() call.
      //
    }
		void SQLViewLoad(object sender, EventArgs e)
		{

		}
		void ListBox1SelectedValueChanged(object sender, EventArgs e)
		{
		  Engine.SQLEventArgs ea = list[listBox1.SelectedIndex];
		  textBox1.Text = ea.SQL;
		  textNumRow.Text = ea.NumLine.ToString();
		  textTime.Text = ea.ElapsedMilliseconds.ToString();
		  listParam.Items.Clear();
		  foreach(var p in ea.Params)
		    listParam.Items.Add(p.Key+"="+p.Value.ToString());
		  
		  listResult.Clear();
		  listResult.Columns.Add("№");
		  if (ea.Result.Count>0)
		  {
  		  foreach(var col in ea.Result[0].Keys)
  		    listResult.Columns.Add(col);
  		  int n=1;
  		  foreach(var row in ea.Result)
  		  {
  		    var item = new ListViewItem((n++).ToString());
  		    foreach(var col in row.Value)
  		    {
  		      var subitem = new ListViewItem.ListViewSubItem();
  		      subitem.Text = col.Value.ToString();
  		      item.SubItems.Add(subitem);
  		    }
  		    listResult.Items.Add(item);
  		  }
		  }
		}
  }
}
